using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.UIElements;
using Microsoft.Practices.CompositeUI.WinForms;
using Microsoft.Practices.ObjectBuilder;
using System.Windows.Forms;

namespace BankTeller.Infrastructure.Module
{

    [Service(typeof(IUIElementBuilder))]
    public class UIElementBuilder : IUIElementBuilder
    {

        private WorkItem _workItem;       

        [ServiceDependency]
        public WorkItem workItem
        {
            get { return _workItem; }
            set { _workItem = value; }
        }


        // Loads the menu items from App.config and put them into the menu strip, hooking
        // up the menu URIs for command dispatching.
        public void LoadFromConfig()
        {
           ShellItemsSection section = (ShellItemsSection)ConfigurationManager.GetSection("shellitems");
            
           foreach (MenuItemElement menuItem in section.MenuItems)
           {
               ToolStripMenuItem uiMenuItem = menuItem.ToMenuItem();

               workItem.UIExtensionSites[menuItem.Site].Add(uiMenuItem);

               if (menuItem.Register == true)
                   workItem.UIExtensionSites.RegisterSite(menuItem.RegistrationSite, uiMenuItem.DropDownItems);

               if (!String.IsNullOrEmpty(menuItem.CommandName))
                   workItem.Commands[menuItem.CommandName].AddInvoker(uiMenuItem, "Click");
           } 
        }
    }
}
